package the.workshop.milktea.interfaces.controller;


import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.wisteria.framework.util.DateUtils;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.auth.UserContext;
import the.workshop.milktea.infrastructure.model.DeviceCollect;
import the.workshop.milktea.interfaces.req.*;
import the.workshop.milktea.interfaces.resp.*;
import the.workshop.milktea.service.DeviceCollectService;
import the.workshop.milktea.service.DeviceService;
import the.workshop.milktea.service.FirmwareService;
import the.workshop.milktea.service.dto.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 设备 前端控制器
 * </p>
 *
 * @author huamu
 * @since 2023-04-01
 */
@Api(tags = "Tea")
@RestController
@RequestMapping("/milktea/device")
public class DeviceController {
    @Resource
    private DeviceService deviceService;

    @Resource
    private DeviceCollectService deviceCollectService;

    @PostMapping("/add")
    public APIResult<Boolean> add(@RequestBody DeviceAddReq req) {
        DeviceAddDTO dto = new DeviceAddDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(deviceService.add(dto));
    }

    @PostMapping("/modify")
    public APIResult<Boolean> modify(@RequestBody DeviceModifyReq req) {
        DeviceModifyDTO dto = new DeviceModifyDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(deviceService.modify(dto));
    }

    @PostMapping("/remove")
    public APIResult<Boolean> remove(@RequestBody DeviceRemoveReq req) {
        deviceService.removeByIds(req.getIds(), UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @GetMapping("/page")
    public APIResult<QueryResult<DevicePageResultDTO>> page(DevicePageReq req) {
        DevicePageDTO dto  = new DevicePageDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setPageNum(req.getPageNum());
        dto.setPageSize(req.getPageSize());
        QueryResult queryResult = deviceService.page(dto);
        return APIResult.success(queryResult);
    }

    @GetMapping("/{id}")
    public APIResult<DeviceGetResp> get(@PathVariable(value = "id") Integer id) {
        DeviceGetResp resp = null;
        DeviceGetDTO dto = deviceService.get(id);
        if (dto != null) {
            resp = new DeviceGetResp();
            BeanUtils.copyProperties(dto, resp);
        }
        return APIResult.success(resp);
    }
    @GetMapping("/get-by-mac/{mac}")
    public APIResult<DeviceGetResp> getByMac(@PathVariable(value = "mac") String mac) {
        DeviceGetResp resp = null;
        DeviceGetDTO dto = deviceService.getByMac(mac);
        if (dto != null) {
            resp = new DeviceGetResp();
            BeanUtils.copyProperties(dto, resp);
        }
        return APIResult.success(resp);
    }

    @PostMapping("/lock")
    public APIResult<Boolean> lock(@RequestBody @Valid DeviceLockStatusReq req) {
        DeviceLockStatusModifyDTO dto = new DeviceLockStatusModifyDTO();
        dto.setMac(req.getMac());
        dto.setLockStatus(1);
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(deviceService.modifyLockStatus(dto));
    }

    @PostMapping("/unlock")
    public APIResult<Boolean> unlock(@RequestBody @Valid DeviceLockStatusReq req) {
        DeviceLockStatusModifyDTO dto = new DeviceLockStatusModifyDTO();
        dto.setMac(req.getMac());
        dto.setLockStatus(0);
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(deviceService.modifyLockStatus(dto));
    }

    @GetMapping("/collect/report")
    public APIResult<QueryResult<DeviceCollectReportResp>> report() {
        List<DeviceCollectReportResp> dataList = deviceCollectService.report();
        return APIResult.success(new QueryResult<>(dataList.size(), dataList));
    }
}
